package com.tanhua.dubbomongo.api;

import com.tanhua.dubbointerface.api.CommentApi;
import com.tanhua.dubbointerface.api.MovementApi;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.utills.CommentType;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@DubboService
public class CommentApiImpl implements CommentApi {

    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private MovementApi movementApi;
    @Override
    public Integer save(Comment comment,String movementId) {
        //向评论列表中添加数据
        Movement movement = movementApi.getMovementByID(movementId);

        comment.setCreated(System.currentTimeMillis());
        Query commentQuery = new Query(Criteria.where("_id").is(new ObjectId(movementId)));
        Comment one = mongoTemplate.findOne(commentQuery, Comment.class);

            comment.setPublishUserId(movement.getUserId());
            comment.setPublishId(movement.getId());



        mongoTemplate.save(comment);
        Query query = new Query(Criteria.where("id").is(comment.getPublishId()));
        //修改动态数据中的评论数或者点赞数或则喜欢数
        Update update = new Update();
        if (comment.getCommentType()== CommentType.LIKE.getType()){
            update.inc("likeCount",1);
        }else if (comment.getCommentType()==CommentType.COMMENT.getType()){
            update.inc("commentCount",1);
        }else {
            update.inc("loveCount",1);
        }
        FindAndModifyOptions options =new FindAndModifyOptions();
        options.returnNew(true);
        Movement andModify = mongoTemplate.findAndModify(query, update, options,Movement.class);
        return andModify.statisCount(comment.getCommentType());
    }




    @Override
    public Integer delete(Comment comment, String id) {
        //1、删除Comment表数据
        Criteria criteria = Criteria.where("userId").is(comment.getUserId())
                .and("publishId").is(new ObjectId(id))
                .and("commentType").is(comment.getCommentType());
        Query query = Query.query(criteria);
        mongoTemplate.remove(query,Comment.class);
        Query queryUpdate = new Query(Criteria.where("_id").is(new ObjectId(id)));
        //修改动态数据中的评论数或者点赞数或则喜欢数
        Update update = new Update();
        if (comment.getCommentType()== CommentType.LIKE.getType()){
            update.inc("likeCount",-1);
        }else if (comment.getCommentType()==CommentType.COMMENT.getType()){
            update.inc("commentCount",-1);
        }else {
            update.inc("loveCount",-1);
        }
        FindAndModifyOptions options =new FindAndModifyOptions();
        options.returnNew(true);
        Movement andModify = mongoTemplate.findAndModify(queryUpdate, update, options,Movement.class);
        Integer count = andModify.statisCount(comment.getCommentType());
        //删除评论表中的点赞信息
        return count;
    }

    @Override
    public boolean hasComment(String id, Long userId, CommentType like) {
        Query query = new Query(Criteria.where("publishId").is(new ObjectId(id)).and("userId").is(userId).and("commentType").is(like.getType()));
        Movement one = mongoTemplate.findOne(query, Movement.class);
        return one==null;
    }

    /**
     * 查询对应动态的评论信息
     * @param publishId
     * @param type
     * @param page
     * @param pagesize
     * @return
     */
    @Override
    public List<Comment> findByPublishId(String publishId, int type, int page, int pagesize) {
        Query query = new Query(Criteria.where("publishId").is(new ObjectId(publishId)).and("commentType")
                .is(type))
                .skip((page-1)*pagesize)
                .limit(pagesize)
                .with(Sort.by(Sort.Order.desc("created")));
        List<Comment> comments = mongoTemplate.find(query, Comment.class);
        return comments;
    }

    @Override
    public Integer deleteCommentLike(Comment comment, String id) {
        //1、删除Comment表数据
        Criteria criteria = Criteria.where("userId").is(comment.getUserId())
                .and("publishId").is(new ObjectId(id))
                .and("commentType").is(comment.getCommentType());
        Query query = Query.query(criteria);
        mongoTemplate.remove(query,Comment.class);
        Query queryUpdate = new Query(Criteria.where("_id").is(new ObjectId(id)));
        //修改动态数据中的评论数或者点赞数或则喜欢数
        Update update = new Update();
        update.inc("likeCount",-1);
        FindAndModifyOptions options =new FindAndModifyOptions();
        options.returnNew(true);
        Comment andModify = mongoTemplate.findAndModify(queryUpdate, update, options, Comment.class);
        return andModify.getLikeCount();

    }

    @Override
    public Integer saveCommentLike(Comment comment, String id) {
        comment.setCreated(System.currentTimeMillis());
        Query commentQuery = new Query(Criteria.where("_id").is(new ObjectId(id)));
        Comment one = mongoTemplate.findOne(commentQuery, Comment.class);

        comment.setPublishUserId(one.getUserId());
        comment.setPublishId(one.getId());

        mongoTemplate.save(comment);
        Query query = new Query(Criteria.where("id").is(comment.getPublishId()));
        //修改动态数据中的评论数或者点赞数或则喜欢数
        Update update = new Update();

        update.inc("likeCount",1);

        FindAndModifyOptions options =new FindAndModifyOptions();
        options.returnNew(true);
        Comment andModify = mongoTemplate.findAndModify(query, update, options, Comment.class);
        return andModify.getLikeCount();
    }
}